package com.fjxd.ticketStore.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjxd.ticketStore.mapper.TicketStoreMapper;
import com.fjxd.ticketStore.pojo.TicketStore;
import com.fjxd.ticketStore.service.TicketStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author csw
 */
@Slf4j
@Service
public class TicketStoreServiceImpl extends ServiceImpl<TicketStoreMapper, TicketStore> implements TicketStoreService {

    private final TicketStoreMapper ticketStoreMapper;

    public TicketStoreServiceImpl(TicketStoreMapper ticketStoreMapper) {
        this.ticketStoreMapper = ticketStoreMapper;
    }


    @Override
    public void ticketDeduct(String ticketId, Integer ticketType, Integer ticketCount) {
        log.info("扣除机票业务开始");
        try {
            ticketStoreMapper.deduct(ticketId, ticketType, ticketCount);
        } catch (Exception exception) {
            throw new RuntimeException("扣除机票数量失败，机票数量可能库存不足", exception);
        }
        log.info("扣除机票业务结束，扣除成功");
    }

    @Override
    public void ticketGoBack(Integer aircraftTicketId, Integer ticketCount) {
        log.info("改签机票业务一开始");
        log.info("回增机票业务开始");
        try {
            ticketStoreMapper.goBack(aircraftTicketId, ticketCount);
        } catch (Exception exception) {
            throw new RuntimeException("出现未知故障", exception);
        }
        log.info("回增成功，改签机票业务二开始");
    }


    @Override
    public List<TicketStore> getTicketCount(Integer ticketId) {
        return ticketStoreMapper.getTicketCount(ticketId);
    }
}
